/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*-  */
/*
 * catchit
 * Copyright (C) 2013 Ryan Grinkewitz <graphite@Toto>
 * 
 */

#include "level.h"
#include <iostream>
#include <iomanip>


Level::Level(int levelSize){

	if(levelSize >= 3 || levelSize <= 20){
		this->levelSize = levelSize;		
	}else{
		//invalid entry, using default level size
		this->levelSize = DEFAULT_LEVEL_SIZE;
	}

	platforms = new Platform* [this->levelSize];

	for(int i = 0; i < levelSize; i++){
		platforms[i] = NULL;
	}

	yPosition = -1;
}

Level::~Level(){

	for(int i = 0; i < levelSize; i++){
		Platform* platform = getPlatform(i);
		if(platform != NULL){
			delete platform;
			platform = NULL;
		}
		
	}
	
}

void Level::updateLevelYPosition(int yPosition){
		this->yPosition = yPosition;
	for(int i = 0; i < LEVEL_SIZE; i++){
		if(platforms[i] != NULL){
			platforms[i]->setPlatformYPosition(yPosition);
		}
	}
}

Platform* Level::getPlatform(int index){
	return platforms[index];
}

void Level::insertPlatform(Platform* platform, int index){
	if(index < levelSize){
		platforms[index] = platform;
	}else{
		//log it?
	}
}

int Level::getLevelState(){
	int levelState = 0;
	
	for(int i = 0; i < LEVEL_SIZE; i++){
		levelState <<= 1;

		if(platforms[i] != NULL){
			levelState |= 1; 
		}
	}

	std::cout << std::setbase(10) << "levelState:" << levelState << std::endl;
	return levelState;
}